package vamp_serviceweb

import org.springframework.dao.DataIntegrityViolationException

class TransitController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [transitInstanceList: Transit.list(params), transitInstanceTotal: Transit.count()]
    }

    def create() {
        [transitInstance: new Transit(params)]
    }

    def save() {
        def transitInstance = new Transit(params)
        if (!transitInstance.save(flush: true)) {
            render(view: "create", model: [transitInstance: transitInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'transit.label', default: 'Transit'), transitInstance.id])
        redirect(action: "show", id: transitInstance.id)
    }

    def show() {
        def transitInstance = Transit.get(params.id)
        if (!transitInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'transit.label', default: 'Transit'), params.id])
            redirect(action: "list")
            return
        }

        [transitInstance: transitInstance]
    }

    def edit() {
        def transitInstance = Transit.get(params.id)
        if (!transitInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'transit.label', default: 'Transit'), params.id])
            redirect(action: "list")
            return
        }

        [transitInstance: transitInstance]
    }

    def update() {
        def transitInstance = Transit.get(params.id)
        if (!transitInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'transit.label', default: 'Transit'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (transitInstance.version > version) {
                transitInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'transit.label', default: 'Transit')] as Object[],
                        "Another user has updated this Transit while you were editing")
                render(view: "edit", model: [transitInstance: transitInstance])
                return
            }
        }

        transitInstance.properties = params

        if (!transitInstance.save(flush: true)) {
            render(view: "edit", model: [transitInstance: transitInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'transit.label', default: 'Transit'), transitInstance.id])
        redirect(action: "show", id: transitInstance.id)
    }

    def delete() {
        def transitInstance = Transit.get(params.id)
        if (!transitInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'transit.label', default: 'Transit'), params.id])
            redirect(action: "list")
            return
        }

        try {
            transitInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'transit.label', default: 'Transit'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'transit.label', default: 'Transit'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
